const config = require('../../common/config');
let userId;
let shopId;

Page({
    data: {
        shopInfo: {},
        SHOP_IMAGES: [],
        isCollected:false,
        showMoreShopInfo:false
    },
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '商家'
        })
        userId=wx.getStorageSync(config.USER_ID);
        var _this = this;
        shopId = options.shop_id;
        wx.showLoading({
            title: '加载中',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask: true
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'wxShopManageService',
                methodName: 'selectShopByShopId',
                reqData: JSON.stringify({shopId: shopId})
            },
            success: function (res) {
                var resultData = res.data;
                if (resultData.code == 1) {
                    _this.setData({
                        shopInfo: resultData.data.shopInfo,
                        SHOP_IMAGES: resultData.data.shopImgs
                    })

                    //查询是否收藏
                    wx.request({
                        url: config.COMMPT_REQPATH,
                        header: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        method: 'POST',
                        data: {
                            accessToken: '',
                            refreshToken: '',
                            serviceName: 'collectService',
                            methodName: 'getUserCollect',
                            reqData: JSON.stringify({
                                collectId: shopId,
                                type:'01',
                                userId:userId
                            })
                        },
                        success: function (res) {
                            var resultData = res.data;
                            if (resultData.code == 1) {
                                if(resultData.data.isCollected>0){
                                    _this.setData({
                                        isCollected:true
                                    })
                                }
                            }
                        }
                    })
                }
            },
            complete:function () {
                wx.hideLoading();
            }
        })
    },
    callPhone: function (e) {
        wx.makePhoneCall({
            phoneNumber: this.data.shopInfo.LINK_MOBILE
        })
    },
    toAddressMap: function (e) {
        //打开微信内置地图
        wx.openLocation({
            latitude: this.data.shopInfo.LATITUDE,
            longitude: this.data.shopInfo.LONGITUDE,
            scale: 28
        })
    },
    changeCollect:function (e) {
        this.setData({
            isCollected : e.detail.value
        })

        //切换收藏
        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'collectService',
                methodName: 'setUserCollect',
                reqData: JSON.stringify({
                    collectId: shopId,
                    type:'01',
                    userId:userId,
                    collected:e.detail.value
                })
            },
            success: function (res) {

            }
        })
    },
    moreShopInfo:function (e) {
        this.setData({
            showMoreShopInfo:true
        })
    },
    previewShopImages: function (e) {
        var imgPath = [];
        for(var i = 0;i<this.data.SHOP_IMAGES.length;i++){
            imgPath.push(this.data.SHOP_IMAGES[i].filePath);
        }
        wx.previewImage({
            current: e.currentTarget.dataset.id, // 当前显示图片的http链接
            urls:imgPath// 需要预览的图片http链接列表
        })
    },
})